package com.github.yoojia.mqtt;

public class Context {

    private SocketListener mSocketListener;
    private String mServiceDomain;
    private String mClientChannel;
    private String mUsername;
    private String mPassword;
    private String mAddress;
    private int mTimeout;
    private boolean mAutoReconnect = true; // 默认值
    private boolean mClearSession = true; // 默认值
    private boolean mUnsubscribeOnDisconnect = true; // 默认值
    private int mWorkerId = 0; // 默认值
    private int mDataCenterId = 0; // 默认值

    /**
     * Socket状态监听接口
     * @param listener 监听接口
     * @return Context
     */
    public Context socketListener(SocketListener listener){
        mSocketListener = listener;
        return this;
    }
    /**
     * 设置服务域
     * @param serviceDomain 服务域名称
     * @return Context
     */
    public Context domain(String serviceDomain) {
        mServiceDomain = serviceDomain;
        return this;
    }

    /**
     * 设置终端的客户端的ChannelName
     * @param channelName ChannelName
     * @return Context
     */
    public Context nodeId(String channelName) {
        mClientChannel = channelName;
        return this;
    }

    /**
     * 设置连接MQTT服务端的认证用户名
     * @param username 认证用户名
     * @return Context
     */
    public Context username(String username) {
        mUsername = username;
        return this;
    }

    /**
     * 设置连接MQTT服务端的认证密码
     * @param password 认证密码
     * @return Context
     */
    public Context password(String password) {
        mPassword = password;
        return this;
    }

    /**
     * MQTT设置。设置连接MQTT服务端的地址
     * @param address 服务端地址，如: tcp://iot.eclipse.org:1883
     * @return Context
     */
    public Context address(String address) {
        mAddress = address;
        return this;
    }

    /**
     * MQTT设置。设置连接MQTT服务端的连接超时时间，单位：秒。默认为60秒。
     * @param timeout 超时时间
     * @return Context
     */
    public Context timeout(int timeout) {
        mTimeout = timeout;
        return this;
    }

    /**
     * MQTT设置。设置MQTT Client是否自动重连。默认为true。
     * @param autoReconnect 是否自动重连
     * @return Context
     */
    public Context autoReconnect(boolean autoReconnect) {
        mAutoReconnect = autoReconnect;
        return this;
    }

    /**
     * MQTT设置。设置MQTT Client是否清除Session。默认为true。
     * @param clearSession 是否清除Session
     * @return Context
     */
    public Context clearSession(boolean clearSession) {
        mClearSession = clearSession;
        return this;
    }

    /**
     * 设置Snowflake分布式自增ID的WorkerId，默认为0。可以根据需要设置以保证分布式生成全局唯一ID。
     * @param workerId WorkerId
     * @return Context
     */
    public Context workerId(int workerId) {
        mWorkerId = workerId;
        return this;
    }

    /**
     * 设置Snowflake分布式自增ID的DataCenterId，默认为0。可以根据需要设置以保证分布式生成全局唯一ID。
     * @param dataCenterId DataCenterId
     * @return Context
     */
    public Context dataCenterId(int dataCenterId) {
        mDataCenterId = dataCenterId;
        return this;
    }

    /**
     * 设置断开连接时，是否取消订阅消息Topic。默认为true
     * @param clearSession 是否取消订阅消息的Topic
     * @return Context
     */
    public Context unsubscribeOnDisconnect(boolean clearSession) {
        mClearSession = clearSession;
        return this;
    }

    public MQTTSocket socket(){
        return new MQTTSocket(new MQTTSocket.Config(
                mServiceDomain, mClientChannel, mUsername, mPassword,
                mAddress, mTimeout, mAutoReconnect, mClearSession,
                mDataCenterId, mWorkerId, mUnsubscribeOnDisconnect,
                mSocketListener));
    }

}